package cn.zyjblogs.server.role.service.impl;

import cn.zyjblogs.server.role.mapper.RolePermissionRelMapper;
import cn.zyjblogs.server.role.po.RolePermissionRelPo;
import cn.zyjblogs.server.role.service.RolePermissionRelService;
import cn.zyjblogs.server.role.vo.RolePermissionRelVo;
import cn.zyjblogs.starter.common.entity.context.BaseContext;
import cn.zyjblogs.starter.common.utils.bean.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 角色权限关联业务层
 *
 * @author zhuyijun
 */
@Service
@RequiredArgsConstructor
public class RolePermissionRelServiceImpl extends ServiceImpl<RolePermissionRelMapper, RolePermissionRelPo> implements RolePermissionRelService {
    private final RolePermissionRelMapper rolePermissionRelMapper;

    /**
     * @param roleId 角色id
     * @return 角色和权限关联表
     * @author zhuyijun
     * @date 2022/9/24 上午11:56
     */
    @Override
    public List<String> findListByRoleId(String roleId) {
        return rolePermissionRelMapper.findListByRoleId(roleId, BaseContext.getTenantId());
    }

    /**
     * @param roleIds 角色id集合查询权限id集合
     * @return 角色和权限关联表
     * @author zhuyijun
     * @date 2022/9/24 上午11:56
     */
    @Override
    public List<String> findListByRoleIds(List<String> roleIds) {
        return rolePermissionRelMapper.findListByRoleIds(roleIds, BaseContext.getTenantId());
    }

    /**
     * @param roleId 角色id
     * @return 角色和权限关联表
     * @author zhuyijun
     * @date 2022/9/24 上午11:56
     */
    @Override
    public List<RolePermissionRelVo> findListVoByRoleId(String roleId) {
        LambdaQueryWrapper<RolePermissionRelPo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RolePermissionRelPo::getRoleId, roleId)
                .eq(RolePermissionRelPo::getTenantId, BaseContext.getTenantId());
        List<RolePermissionRelPo> rolePermissionRelPos = rolePermissionRelMapper.selectList(queryWrapper);
        return BeanUtils.map(rolePermissionRelPos, RolePermissionRelVo.class);
    }

    /**
     * @param roleIds 角色id集合
     * @return 角色和权限关联表
     * @author zhuyijun
     * @date 2022/9/24 上午11:56
     */
    @Override
    public List<RolePermissionRelVo> findListVoByRoleIds(List<String> roleIds) {
        LambdaQueryWrapper<RolePermissionRelPo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(RolePermissionRelPo::getRoleId, roleIds)
                .eq(RolePermissionRelPo::getTenantId, BaseContext.getTenantId());
        List<RolePermissionRelPo> rolePermissionRelPos = rolePermissionRelMapper.selectList(queryWrapper);
        return BeanUtils.map(rolePermissionRelPos, RolePermissionRelVo.class);
    }

    /**
     * 通过角色id删除
     *
     * @param roleId
     * @return
     * @author zhuyijun
     * @date 2022/9/24
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByRoleId(String roleId) {
        LambdaQueryWrapper<RolePermissionRelPo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RolePermissionRelPo::getRoleId, roleId)
                .eq(RolePermissionRelPo::getTenantId, BaseContext.getTenantId());
        return rolePermissionRelMapper.delete(queryWrapper) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByRoleIds(List<String> roleIds) {
        LambdaQueryWrapper<RolePermissionRelPo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(RolePermissionRelPo::getRoleId, roleIds)
                .eq(RolePermissionRelPo::getTenantId, BaseContext.getTenantId());
        return rolePermissionRelMapper.delete(queryWrapper) > 0;
    }

    /**
     * 保存或更新角色权限关联
     *
     * @param roleId
     * @param permissionIds
     * @param needClear
     * @return
     * @author zhuyijun
     * @date 2022/9/24
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveUpdate(String roleId, Collection<String> permissionIds, boolean needClear) {
        if (needClear) {
            deleteByRoleId(roleId);
        }
        if (CollectionUtils.isEmpty(permissionIds)) {
            return true;
        }
        String tenantId = BaseContext.getTenantId();
        List<RolePermissionRelPo> list = new ArrayList<>(permissionIds.size());
        permissionIds.forEach(permissionId -> list.add(RolePermissionRelPo.builder()
                .roleId(roleId)
                .permissionId(permissionId)
                .tenantId(tenantId)
                .build()));
        return this.saveBatch(list);
    }

}
